package com.rightsidetech.http.controller.admin.activity;

import com.github.pagehelper.PageInfo;
import com.rightsidetech.activity.dto.request.ride.ActRideCountReq;
import com.rightsidetech.activity.dto.request.ride.ActRidePageReq;
import com.rightsidetech.activity.dto.request.ride.ByRideActReq;
import com.rightsidetech.activity.dto.request.ride.VerifyRideActReq;
import com.rightsidetech.activity.dto.response.ActRideCountResp;
import com.rightsidetech.activity.dto.response.RideActivityResp;
import com.rightsidetech.activity.entity.ActRideActivity;
import com.rightsidetech.activity.service.ActRideService;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.push.EnumMsgType;
import com.rightsidetech.http.task.MassageTask;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.entity.SysUserLoginInfo;
import com.rightsidetech.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/ride_act/")
@ShenyuSpringMvcClient(path = "/admin/ride_act/**")
@Api(tags = "骑行活动相关")
public class ActRideController extends BaseController {

    @Resource
    private ActRideService actRideService;
    @Autowired
    private MassageTask massageTask;
    @Resource
    private SysUserService sysUserService;

    @Token
    @Log
    @GetMapping("/page")
    @ApiOperation("分页查询骑行活动")
    @Auth(bizType = Auth.OPERATOR)
    public ResponseData<PageInfo<RideActivityResp>> ridePage(ActRidePageReq req){
        Long loginId = Long.parseLong(getAttribute());
        SysUserLoginInfo loginInfo = sysUserService.selectUserById(loginId);
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        if (loginInfo.getSysCompanyAreaId() != 0){
            req.setAreaId(loginInfo.getSysCompanyAreaId());
        }
        return actRideService.page(req);
    }

    @Token
    @Log
    @PostMapping("/by_act")
    @ApiOperation("通过拒绝骑行活动")
    @Auth(bizType = Auth.OPERATOR)
    public ResponseData isByAct(@RequestBody ByRideActReq req) throws Exception{
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        SysUserLoginInfo loginInfo = sysUserService.selectUserById(loginInfoId);
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        ActRideActivity activity = actRideService.selectById(req.getId());
        if (ObjectUtils.isEmpty(activity)){
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }
        //不能操作其他大区数据
        if (loginInfo.getSysCompanyAreaId() != 0 && !loginInfo.getSysCompanyAreaId().equals(activity.getAreaId())){
            return ResponseData.fail("无权操作");
        }
        ResponseData responseData = actRideService.byRideAct(req,loginName, loginInfoId);
        if (responseData.getCodeId() == 1){
//            if (!ObjectUtils.isEmpty(activity)){
                if (activity.getStatus() == 1){
                    massageTask.agentPushActivity(activity.getAgentLoginId(),"骑行活动审核已通过", EnumMsgType.RIDE_ACTIVITY_AGREE.getValue(),String.valueOf(req.getId()));
                }else {
                    massageTask.agentPushActivity(activity.getAgentLoginId(),"骑行活动审核已拒绝", EnumMsgType.RIDE_ACTIVITY_UNAGREE.getValue(),String.valueOf(req.getId()));
                }
//            }
        }
        return responseData;
    }


    @Token
    @Log
    @PostMapping("/verify")
    @ApiOperation("审核骑行活动")
    @Auth(bizType = Auth.OPERATOR)
    public ResponseData verify(@RequestBody VerifyRideActReq req) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        SysUserLoginInfo loginInfo = sysUserService.selectUserById(loginInfoId);
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        ActRideActivity activity = actRideService.selectById(req.getId());
        if (ObjectUtils.isEmpty(activity)){
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }
        //不能操作其他大区数据
        if (loginInfo.getSysCompanyAreaId() != 0 && !loginInfo.getSysCompanyAreaId().equals(activity.getAreaId())){
            return ResponseData.fail("无权操作");
        }
        return actRideService.verifyRideAct(req, loginName, loginInfoId);
    }


    @Token
    @Log
    @GetMapping("/count_page")
    @ApiOperation("骑行活动统计")
    @Auth(bizType = Auth.OPERATOR)
    public ResponseData<PageInfo<ActRideCountResp>> countPage(ActRideCountReq req){
        Long loginInfoId = Long.parseLong(getAttribute());
        SysUserLoginInfo loginInfo = sysUserService.selectUserById(loginInfoId);
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        if (loginInfo.getSysCompanyAreaId() != 0){
            req.setAreaId(loginInfo.getSysCompanyAreaId());
        }
        return actRideService.recordCountPage(req);
    }
}
